package com.yy.wga.mp.module.goods.entity.bo;

import cn.hutool.core.util.StrUtil;
import com.yy.wga.mp.module.goods.manager.ProductManager;
import com.yy.wga.mp.module.goods.response.GoodsInfoResp;
import com.yy.wga.mp.common.dto.UserCacheDTO;
import com.yy.wga.mp.common.interceptor.RequestUserHolder;
import com.yy.wga.mp.common.utils.common.EnvUtils;
import com.yy.wga.mp.common.utils.common.SpringUtils;
import com.yy.wga.mp.common.utils.common.StringUtils;
import com.yy.wga.mp.common.utils.mapping.BeansUtils;
import com.yy.wga.mp.module.goods.api.response.product.PromoteProductDetailDTO;
import com.yy.wga.mp.module.goods.common.utils.CommissionUtil;
import com.yy.wga.mp.module.goods.entity.pojo.GoodsSpuDO;
import com.yy.wga.mp.module.user.enums.EUserGrade;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class GetPromoteGoodsTaskV2BO {

    private List<GoodsIdBO> goodsIdBOList;
    private EUserGrade userGrade;
    private String appKey;
    private Integer playType;
    private Boolean useDb;



    /**
     * 用户商品列表
     *
     * @return
     */
    public List<GoodsInfoResp> execute() {
        if (StringUtils.isNotBlank(appKey)) {
            BaseRequestDTO requestDTO = new BaseRequestDTO();
            requestDTO.setAppKey(appKey);
            RequestUserHolder.setBaseRequest(requestDTO);
        }
        List<GoodsInfoResp> respList = new ArrayList<>();

        List<GoodsIdBO> goodsIdBOS = Optional.ofNullable(goodsIdBOList).orElse(new ArrayList<>());

        if (Boolean.TRUE.equals(useDb)){
            List<GoodsHeadSupplierInfoDO> goodsHeadSupplierInfos = SpringUtils.getBean(GoodsHeadSupplierInfoService.class).getGoodsHeadSupplierInfos(goodsIdBOS);
            for (GoodsHeadSupplierInfoDO goodsHeadSupplierInfo : goodsHeadSupplierInfos){
                GoodsInfoResp goodsInfoResp = toGoodsInfoResp(goodsHeadSupplierInfo, userGrade);
                respList.add(goodsInfoResp);
            }
            return respList;
        }


        ProductManager manager = SpringUtils.getBean(ProductManager.class);

        for (GoodsIdBO goodsIdBO : Optional.ofNullable(goodsIdBOList).orElse(new ArrayList<>())) {
            // 使用公开计划
            PromoteProductDetailDTO.Product detail = manager.getPromoteProductDetail(playType, goodsIdBO);
            if (Objects.nonNull(detail)) {
                GoodsInfoResp goodsInfoResp = toGoodsInfoResp(detail, userGrade);
                respList.add(goodsInfoResp);
            }
        }

        return respList;
    }

    /**
     * 同步商品信息
     *
     * @param productResp productResp
     */
    public static GoodsInfoResp toGoodsInfoResp(PromoteProductDetailDTO.Product productResp, EUserGrade userGrade) {
        GoodsSpuDO goodsSpu = new GoodsSpuDO();
        PromoteProductDetailDTO.ProductInfo productInfo = productResp.getProductInfo();

        int stockNum = 0;
        List<PromoteProductDetailDTO.Sku> skuResps = productInfo.getSkus();
        Integer salePrice = skuResps.get(0).getSalePrice();
        List<GoodsSpuDO.Sku> skuInfos = new ArrayList<>();
        for (PromoteProductDetailDTO.Sku skuResp : skuResps) {
            stockNum += skuResp.getStockNum();
            List<GoodsSpuDO.Attr> goodsAttrs = new ArrayList<>();
            for (PromoteProductDetailDTO.Attr attr : skuResp.getSkuAttrs()) {
                String attrValue = attr.getAttrValue();
                if (StringUtils.isNotBlank(attrValue)) {
                    GoodsSpuDO.Attr goodsAttr = new GoodsSpuDO.Attr();
                    goodsAttr.setAttrKey(attr.getAttrKey());
                    goodsAttr.setAttrValue(Arrays.asList(attrValue.split(";")));
                    goodsAttrs.add(goodsAttr);
                }
            }
            GoodsSpuDO.Sku sku = BeansUtils.map(skuResp, GoodsSpuDO.Sku.class);
            sku.setSkuAttrs(goodsAttrs);
            skuInfos.add(sku);
        }
        PromoteProductDetailDTO.DescInfo descInfo = productInfo.getDescInfo();
        if (Objects.nonNull(descInfo)) {
            goodsSpu.setDetailImg(descInfo.getImgs());
        }
        goodsSpu.setSkus(skuInfos);
        goodsSpu.setShopAppid(productResp.getShopAppid());
        goodsSpu.setSpuId(productResp.getProductId());
        goodsSpu.setTitle(productInfo.getTitle());
        goodsSpu.setSubTitle(productInfo.getSubTitle());
        goodsSpu.setHeadImg(productInfo.getHeadImgs());
        goodsSpu.setStockNum(stockNum);
        goodsSpu.setSalePrice(salePrice);
        goodsSpu.setMarketPrice(salePrice);
        goodsSpu.setProductPromotionLink(productInfo.getProductPromotionLink());
        List<PromoteProductDetailDTO.Cat> catsV2 = productInfo.getCatsV2();
        if (!CollectionUtils.isEmpty(catsV2)) {
            int index = 1;
            for (PromoteProductDetailDTO.Cat cat : catsV2) {
                String catId = cat.getCatId();
                switch (index) {
                    case 1:
                        goodsSpu.setFirstCateId(catId);
                        break;
                    case 2:
                        goodsSpu.setSecondCateId(catId);
                        break;
                    case 3:
                        goodsSpu.setThirdCateId(catId);
                        break;
                }
                index++;
            }
        }
        // 设置为上架状态
        goodsSpu.setStatus(5);
        GoodsInfoResp goodsInfoResp = BeansUtils.map(goodsSpu, GoodsInfoResp.class);
        goodsInfoResp.setHeadImg(goodsSpu.getHeadImg().get(0));

        // 计算佣金
        Integer commissionValue = CommissionUtil.calcCommissionValue(goodsSpu.getSalePrice(), productResp.getCommissionInfo().getServiceRatio(), userGrade);
        goodsInfoResp.setCommissionValue(commissionValue);
        return goodsInfoResp;
    }


    /**
     * 同步商品信息
     *
     */
    public static GoodsInfoResp toGoodsInfoResp(GoodsHeadSupplierInfoDO goodsHeadSupplierInfoDO, EUserGrade userGrade) {
        GoodsSpuDO goodsSpu = new GoodsSpuDO();
        goodsSpu.setDetailImg(goodsHeadSupplierInfoDO.getHeadImg());
        goodsSpu.setSkus(goodsHeadSupplierInfoDO.getSkus());
        goodsSpu.setShopAppid(goodsHeadSupplierInfoDO.getShopAppid());
        goodsSpu.setSpuId(goodsHeadSupplierInfoDO.getSpuId());
        goodsSpu.setTitle(goodsHeadSupplierInfoDO.getTitle());
        goodsSpu.setSubTitle(goodsHeadSupplierInfoDO.getTitle());
        goodsSpu.setHeadImg(goodsHeadSupplierInfoDO.getHeadImg());
        goodsSpu.setStockNum(0);
        goodsSpu.setSalePrice(goodsHeadSupplierInfoDO.getSalePrice());
        goodsSpu.setMarketPrice(goodsHeadSupplierInfoDO.getSalePrice());
        goodsSpu.setProductPromotionLink(StrUtil.EMPTY);
        goodsSpu.setFirstCateId(goodsHeadSupplierInfoDO.getFirstCateId());
        goodsSpu.setSecondCateId(goodsHeadSupplierInfoDO.getSecondCateId());
        goodsSpu.setThirdCateId(goodsHeadSupplierInfoDO.getThirdCateId());
        // 设置为上架状态
        goodsSpu.setStatus(5);
        GoodsInfoResp goodsInfoResp = BeansUtils.map(goodsSpu, GoodsInfoResp.class);

        // 计算佣金
        Integer ratio = goodsHeadSupplierInfoDO.getRatio();
        Integer serviceRatio = goodsHeadSupplierInfoDO.getServiceRatio();

        Integer maxRatio = Math.max(ratio, serviceRatio);
        // 使用 maxRatio 进行后续操作
        Integer commissionValue = CommissionUtil.calcCommissionValue(goodsSpu.getSalePrice(), maxRatio, userGrade);
        goodsInfoResp.setCommissionValue(commissionValue);

        // 商品详情的不要
        goodsInfoResp.setProductPromotionLink(null);
        goodsInfoResp.setHeadImg(goodsSpu.getHeadImg().get(0));
        return goodsInfoResp;
    }

}